package com.hy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hy.bean.User;
import com.hy.dao.LoginDao;
import com.hy.dao.UserDao;
import com.hy.service.LoginService;
import com.hy.service.UserService;
import com.hy.utils.*;
import org.apache.commons.dbutils.DbUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LoginServiceImpl implements LoginService {
    private LoginDao loginDao = new LoginDao();

    @Override
    public void selectLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String phone1 = request.getParameter("phone1");
        String password = request.getParameter("password");
        List<User> users = loginDao.selectLogin(phone1, password);
        ResponseResult<List<User>> result = null;
        if (users.size() > 0) {
            String userName = users.get(0).getUserName();
            Integer status = Integer.valueOf(users.get(0).getUserStatus());
            HttpSession session = request.getSession();
            session.setAttribute("username", userName);
            result = new ResponseResult<>(200, "登录成功", users, status);
        } else {
            result = new ResponseResult<>(200, "登录失败", users, 102);
        }
        ResultUtils.result(result, request, response);
    }

    @Override
    public void selectPhone(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String phone = request.getParameter("phone");
        boolean str = StrUtil.isNotBlank(phone);
        if (str) {
            boolean b = loginDao.selectPhone(phone);
            ResponseResult<Boolean> result = null;
            if (b) {
                result = new ResponseResult<>(200, "手机号已被注册", b, 101);
            } else {
                result = new ResponseResult<>(200, "", b, 102);
            }
            ResultUtils.result(result, request, response);
        }
    }

    @Override
    public void insertUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String phone = request.getParameter("phone");
        String password1 = request.getParameter("password1");
        String username = RandomUsername.generateRandomUsername();
        boolean phoneStr = StrUtil.isNotBlank(phone);
        boolean password1Str = StrUtil.isNotBlank(password1);
        if (phoneStr && password1Str) {
            boolean b = loginDao.insertUser(new User(0, username, password1, phone,null,null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null));
            ResponseResult<Boolean> result = null;
            if (b) {
                result = new ResponseResult<>(200, "注册成功", b, 101);
            } else {
                result = new ResponseResult<>(200, "注册失败", b, 102);
            }
            ResultUtils.result(result, request, response);
        }
    }

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username1 = request.getParameter("username");
        ResponseResult result = null;
        if (!StrUtil.isBlank(username1)){
            HttpSession session = request.getSession(false);
            if (session != null){
                Object username = session.getAttribute("username");
                if (username.equals(username1)){
                    session.invalidate();

                    result = new ResponseResult<>(200, "ok", true, 101);
                }else {
                    result = new ResponseResult<>(200, "ok", false, 101);
                }
                //removeAttribute()方法。该方法是用来删除session对象中保存的指定属性信息。
//                invalidate()是清除当前session的所有相关信息
            }else {
                result = new ResponseResult<>(200, "ok", false, 101);
            }
        }else {
            result = new ResponseResult<>(200, "ok", false, 101);
        }
        ResultUtils.result(result,request,response);
    }


}
